package com.hmall.api.config;

import cn.hutool.core.util.ObjUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.client.fallback.ItemClientFallbackFactory;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {

    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor() {
        return requestTemplate -> {
            // 传递用户信息
            // 从UserContext中获取用户信息
            // 传递给下游服务
            Long userInfo = UserContext.getUser();
            if (ObjUtil.isNotEmpty(userInfo)) {
                requestTemplate.header("user-info", userInfo.toString());
            }

        };
    }

    // 注入ItemClient的fallbackFactory
    @Bean
    public ItemClientFallbackFactory itemClientFallbackFactory() {
        return new ItemClientFallbackFactory();
    }


}
